package org.dtrd.modules.questionnaire.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dtrd.config.system.Constant;
import org.dtrd.modules.archive.entity.po.DtrdEntRdArchivePhysical;
import org.dtrd.modules.archive.service.IDtrdEntRdPhysicalService;
import org.dtrd.modules.evaluation.service.IDtrdEntRdEvaluationService;
import org.dtrd.modules.archive.entity.po.DtrdEntRdArchiveBasic;
import org.dtrd.modules.patient.entity.po.DtrdEntRdPatient;
import org.dtrd.modules.archive.service.IDtrdEntRdArchiveBasicService;
import org.dtrd.modules.patient.service.IDtrdEntRdPatientService;
import org.dtrd.modules.questionnaire.entity.bean.QuestionnaireLifewayDetail;
import org.dtrd.modules.questionnaire.entity.po.DtrdEntQuestionnaireRdLifeway;
import org.dtrd.modules.questionnaire.mapper.DtrdEntQuestionnaireRdLifewayMapper;
import org.dtrd.modules.questionnaire.service.IDtrdEntQuestionnaireRdLifewayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.Optional;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author QR
 * @since 2022-03-01
 */
@DS("multi-datasource1")
@Service
public class DtrdEntQuestionnaireRdLifewayServiceImpl extends ServiceImpl<DtrdEntQuestionnaireRdLifewayMapper, DtrdEntQuestionnaireRdLifeway> implements IDtrdEntQuestionnaireRdLifewayService {

    @Autowired
    private IDtrdEntRdPhysicalService physicalService;
    @Autowired
    private IDtrdEntRdPatientService patientService;
    @Autowired
    private IDtrdEntRdArchiveBasicService basicService;
    @Autowired
    private IDtrdEntRdEvaluationService evaluationService;

    @Override
    public QuestionnaireLifewayDetail getQuestionnaireRdLifeway(Integer patientId) {
        // patient 基础信息
        DtrdEntRdPatient patient = patientService.getById(patientId);
        // 获取最新的问卷信息
        LambdaQueryWrapper<DtrdEntQuestionnaireRdLifeway> wrapper = Wrappers.lambdaQuery(DtrdEntQuestionnaireRdLifeway.class)
                .eq(DtrdEntQuestionnaireRdLifeway::getPatientId, patientId)
                .orderByDesc(DtrdEntQuestionnaireRdLifeway::getCreateTime)
                .last("limit 1");
        DtrdEntQuestionnaireRdLifeway lifeway = getOne(wrapper);
        // 体格：身高/体重
        DtrdEntRdArchivePhysical physical = physicalService.getOne(Wrappers.lambdaQuery(DtrdEntRdArchivePhysical.class)
                .eq(DtrdEntRdArchivePhysical::getPatientId, patientId));
        // basic 详情
        DtrdEntRdArchiveBasic basic = basicService.getOne(Wrappers.lambdaQuery(DtrdEntRdArchiveBasic.class)
                .eq(DtrdEntRdArchiveBasic::getPatientId, patientId));
        return Optional.ofNullable(patient)
                        .map(po -> {
                            QuestionnaireLifewayDetail dto = new QuestionnaireLifewayDetail().parseFromPO(po);
                            Optional.ofNullable(lifeway).map(dto::parseFromPO);
                            Optional.ofNullable(physical).map(dto::parseFromPO);
                            Optional.ofNullable(basic).map(dto::parseFromPO);
                            return dto;
                        })
                        .orElse(null);
    }

    @Override
    public boolean saveOrUpdateQuestionnaireRdLifeway(QuestionnaireLifewayDetail dto) {
        Integer patientId = dto.getPatientId();
        DtrdEntQuestionnaireRdLifeway questionnaireLifeway = new DtrdEntQuestionnaireRdLifeway();
        if(dto.getDataId() == null){
            questionnaireLifeway = questionnaireLifeway.init();
        }
        // 更新 patient 表
        DtrdEntRdPatient patient = new DtrdEntRdPatient().parseFromDTO(dto);
        patientService.updateById(patient);
        // 体格检查数据
        DtrdEntRdArchivePhysical physical = new DtrdEntRdArchivePhysical().parseFromDTO(dto);
        physicalService.saveOrUpdateByPatientId(physical, patientId);
        // basic 表
        DtrdEntRdArchiveBasic basic = new DtrdEntRdArchiveBasic().parseFromDTO(dto);
        basicService.update(basic, Wrappers.lambdaUpdate(DtrdEntRdArchiveBasic.class)
                .eq(DtrdEntRdArchiveBasic::getPatientId, patientId));
        // 设置 score
        int score = 0;
        String input = dto.getInput();
        String[] answers = input.split(",");
        for (int i = 0; i < answers.length; i++) {
            if (i == 1 || i == 2 || i == 5 || i == 6 || i == 13 || i == 15) {
                switch (answers[i]) {
                    case "A":
                        score +=3;
                        break;
                    case "B":
                        score += 2;
                        break;
                    case "C":
                        score += 1;
                        break;
                }
            }
            if (i == 0 || i == 3 || i == 4 || i == 7 || i == 8 || i == 9 || i == 12 || i == 14) {
                switch (answers[i]) {
                    case "A":
                        score +=2;
                        break;
                    case "B":
                        score += 1;
                        break;
                }
            }
            if (i == 10 || i == 11) {
                if ("A".equals(answers[i])) {
                    score += 1;
                }
            }
        }
        // https://shimo.im/docs/25q5MY700riopLqD
        HashSet<String> evaluations = new HashSet<>();
        for (int i = 0; i < answers.length; i++) {
            if (answers[i].equals("A") || answers[i].equals("B")) {
                if (i == 0 || i == 1 || i == 2 || i == 3 || i == 4) {
                    evaluations.add(Constant.Evaluation.SDSCA.getName());
                    evaluations.add(Constant.Evaluation.CDMSES.getName());
                    evaluations.add(Constant.Evaluation.ADKnowl.getName());
                }
                if (i == 5) {
                    evaluations.add(Constant.Evaluation.SDSCA.getName());
                    evaluations.add(Constant.Evaluation.CDMSES.getName());
                }
                if (i == 6) {
                    evaluations.add(Constant.Evaluation.SDSCA.getName());
                    evaluations.add(Constant.Evaluation.CDMSES.getName());
                    evaluations.add(Constant.Evaluation.DIABETES.getName());
                }
                if (i == 7) {
                    evaluations.add(Constant.Evaluation.DCP.getName());
                }
                if (i == 8) {
                    evaluations.add(Constant.Evaluation.CVD.getName());
                }
                if (i == 9) {
                    evaluations.add(Constant.Evaluation.CVD.getName());
                    evaluations.add(Constant.Evaluation.ADKnowl.getName());
                }
                if (i == 10 || i == 11) {
                    evaluations.add(Constant.Evaluation.DCP.getName());
                    evaluations.add(Constant.Evaluation.CVD.getName());
                }
                if (i == 12 || i == 13) {
                    evaluations.add(Constant.Evaluation.MMAS8.getName());
                    evaluations.add(Constant.Evaluation.BMQ.getName());
                }
                if (i == 14) {
                    evaluations.add(Constant.Evaluation.PSQI.getName());
                    evaluations.add(Constant.Evaluation.HAPPY.getName());
                    evaluations.add(Constant.Evaluation.SAS.getName());
                }
                if (i == 15) {
                    evaluations.add(Constant.Evaluation.PSQI.getName());
                    evaluations.add(Constant.Evaluation.HAPPY.getName());
                    evaluations.add(Constant.Evaluation.SAS.getName());
                    evaluations.add(Constant.Evaluation.JOB.getName());
                }
            }else if (i == 6 || answers[i].equals("C")) { // 第六题是 ABC选项
                evaluations.add(Constant.Evaluation.SDSCA.getName());
                evaluations.add(Constant.Evaluation.CDMSES.getName());
                evaluations.add(Constant.Evaluation.DIABETES.getName());
            }
        }
        questionnaireLifeway.parseFromDTO(dto);
        questionnaireLifeway.setScore(score);
        questionnaireLifeway.setResult(evaluations.toString());
        return saveOrUpdate(questionnaireLifeway);
    }

    @Override
    public boolean removeQuestionnaireRdLifeway(Integer dataId) {
        return removeById(dataId);
    }

    private LambdaQueryWrapper<DtrdEntQuestionnaireRdLifeway> baseQueryWrapper() {
        return Wrappers.lambdaQuery(DtrdEntQuestionnaireRdLifeway.class)
                        .eq(DtrdEntQuestionnaireRdLifeway::getIsDel, 0);
    }

    private LambdaUpdateWrapper<DtrdEntQuestionnaireRdLifeway> baseUpdateWrapper() {
        return Wrappers.lambdaUpdate(DtrdEntQuestionnaireRdLifeway.class)
                        .eq(DtrdEntQuestionnaireRdLifeway::getIsDel, 0);
    }
}
